/**
 * 
 */
package com.alkandros.calculator.ui.requests;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

import com.alkandros.calculator.R;
import com.alkandros.calculator.constants.IAppConstants;
import com.alkandros.calculator.manager.CustomProgressDialog;
import com.alkandros.calculator.manager.SelectorManager;
import com.alkandros.calculator.manager.Utils;
import com.alkandros.calculator.validator.EmailValidator;
import com.alkandros.calculator.validator.Form;
import com.alkandros.calculator.validator.NotEmptyValidator;
import com.alkandros.calculator.validator.Validate;

/**
 * @author Jiju Induchoodan
 *
 */
public class RequestFragment extends Fragment implements OnClickListener,IAppConstants{
	private View rootView;
	private EditText edtFirstName;
	private EditText edtSecondName;
	private EditText edtEmail;
	private EditText edtContent;
	private Context context;
	private Button btnSubmit;
	private WebView webViewRequest;
	private Button btnDetailedForm;
	Form mForm ;
	CustomProgressDialog progress;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
	{
		 rootView = inflater.inflate(R.layout.fragment_requests, container, false);
		 context = getActivity();
		 initialiseUI();
		 return rootView;
	}
	
	private void initialiseUI()
	{
		edtFirstName  	= (EditText)rootView.findViewById(R.id.edt_first_name);
		edtSecondName  	= (EditText)rootView.findViewById(R.id.edt_second_name);
		edtEmail	  	= (EditText)rootView.findViewById(R.id.edt_email);
		edtContent		= (EditText)rootView.findViewById(R.id.edt_enquiry);
		btnSubmit   	= (Button)rootView.findViewById(R.id.btn_submit);
		btnDetailedForm = (Button)rootView.findViewById(R.id.btn_request);
		
		Validate firstName	   = new Validate(edtFirstName);
		Validate secondName    = new Validate(edtSecondName);
		Validate email		   = new Validate(edtEmail);
		Validate content	   = new Validate(edtContent);
		
		firstName.addValidator(new NotEmptyValidator(context));
		secondName.addValidator(new NotEmptyValidator(context));
		content.addValidator(new NotEmptyValidator(context));
		email.addValidator(new NotEmptyValidator(context));
		email.addValidator(new EmailValidator(context));
		
		mForm = new Form();
		mForm.addValidates(firstName);
		mForm.addValidates(secondName);
		mForm.addValidates(email);
		mForm.addValidates(content);
		
		btnSubmit.setOnClickListener(this);
		btnDetailedForm.setOnClickListener(this);
		Utils.setBackground(context, 
				btnSubmit, 
				SelectorManager.
				getButtonDrawableByScreenCathegory(context, 
						R.drawable.button_bg, R.drawable.button_press));
		Utils.setBackground(context, 
				btnDetailedForm, 
				SelectorManager.
				getButtonDrawableByScreenCathegory(context, 
						R.drawable.button_bg, R.drawable.button_press));
	}
	@Override
	public void onClick(View v) 
	{
		if(v==btnSubmit)
		{
			if(mForm.validate())
			{
			    // success statement
				sendMail();
			}
			else
			{
			    // error statement like toast, crouton, ...
			}
		}
		else if(v==btnDetailedForm)
		{
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse(REQUEST_FORM));
			startActivity(intent);
		}
		
	}
	
	private void sendMail()
	{
		final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		
		String text = "First Name:"+edtFirstName.getText().toString()
					   +"\n"+"Second Name:"+edtSecondName.getText().toString()
					   +"\n"+"Email:"+edtEmail.getText().toString()
					   +"\n"+"Enquiry "+edtContent.getText().toString();
		/* Fill it with Data */
		emailIntent.setType("plain/text");
		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{getString(R.string.email_id)});
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Enquiry");
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,text);

		/* Send it off to the Activity-Chooser */
		context.startActivity(Intent.createChooser(emailIntent, "Send mail.."));
	}

}
